package com.system.controller;

import com.system.model.SysUser;
import com.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sysUserController")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询用户列表
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysUser
     * @return
     */
    @RequestMapping("/sysUserList")
    public String sysUserList(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser){
        //接收数据
        //执行业务
        //跳转页面
        List<SysUser> sysUserList=sysUserService.queryAllSysUser();
        session.setAttribute("sysUserList",sysUserList);
        return "view/system/sysUser/sysUserList";
    }

    /**
     * 用户添加
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysUser
     * @return
     */
    @RequestMapping("/sysUserAdd")
    public String sysUserAdd(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser){
        //接收数据
        //执行业务
        //跳转页面
        int i=sysUserService.sysUserAdd(sysUser);
        if(i!=0){
            List<SysUser> sysUserList=sysUserService.queryAllSysUser();
            session.setAttribute("sysUserList",sysUserList);
            return "view/system/sysUser/sysUserList";
        }else{
            model.addAttribute("message","添加用户失败！");
            return "sysUserAdd3";
        }
    }

    /**
     * 删除用户信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysUser
     * @param sysUserIdStr
     * @return
     */
    @RequestMapping("/sysUserDelete")
    public String sysUserDelete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser, String sysUserIdStr) {
        //接收数据
        //执行业务
        //跳转页面
        int i = sysUserService.sysUserDelete(sysUserIdStr);
        if (i != 0) {
            List<SysUser> sysUserList = sysUserService.queryAllSysUser();
            session.setAttribute("sysUserList", sysUserList);
            return "view/system/sysUser/sysUserList";
        } else {
            model.addAttribute("message", "添加用户失败！");
            return "view/system/sysUser/sysUserList";
        }
    }

    //4.1修改UI
    @RequestMapping("/sysUserUpdateUI")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser, String sysUserIdStr) {
        //1.接收数据
        //3.调用业务
        //字符类型转数字类型
        SysUser sysUser_db = sysUserService.querySysUserByUserId(Integer.parseInt(sysUserIdStr));
       if(sysUser_db != null){
           model.addAttribute("sysUser_db", sysUser_db);
           //2.跳转页面
           return "view/system/sysUser/sysUserUpdate";
       }else{
           return "view/system/sysUser/sysUserList";
       }
    }

    /**
     * 修改用户信息
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysUser
     * @return
     */
    @RequestMapping("/sysUserUpdate")
    public String sysUserUpdate(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser) {
        //接收数据
        //执行业务
        //跳转页面
        int i = sysUserService.sysUserUpdate(sysUser);
        if (i != 0) {
            List<SysUser> sysUserList = sysUserService.queryAllSysUser();
            session.setAttribute("sysUserList", sysUserList);
            return "view/system/sysUser/sysUserList";
        } else {
            model.addAttribute("message", "修改用户失败！");
            return "view/system/sysUser/sysUserUpdate";
        }
    }

}
